package com.yerbamateprimer.action.system.impl.advice;

import com.google.common.base.Preconditions;
import com.yerbamateprimer.action.system.SystemAction;
import com.yerbamateprimer.advice.Advice;
import com.yerbamateprimer.advice.impl.MateDrunkAdvice;
import com.yerbamateprimer.client.api.ClientApi;
import com.yerbamateprimer.model.User;
import com.yerbamateprimer.round.Round;
import com.yerbamateprimer.round.state.State;
import com.yerbamateprimer.round.state.StateName;

/**
 * This action models the system
 * action, that sends an advice 
 * to the primer, indicating
 * that the mate is drunk and
 * has to be claimed.
 * 
 * @author Matias Liwski
 * @version 1.0
 */
public class SendMateDrunkToPrimer implements SystemAction {
	private ClientApi clientApi;

	/* (non-Javadoc)
	 * @see com.yerbamateprimer.action.system.SystemAction#execute(com.yerbamateprimer.round.Round)
	 */
	@Override
	public void execute(Round round) {
		Preconditions.checkArgument(round != null, "This action needs a round to execute it.");
		State currentState = round.getCurrentState();
		Preconditions.checkState(currentState != null, "There's no current state detected for this round.");
		Preconditions.checkState(StateName.DRINKING.equals(currentState.getName()), "This actions could only be executed at DRINKING state (state: " + currentState.getName() + ").");
		
		User receptor = round.getPrimer();
		User drinker = round.getCurrentDrinker();
		Advice mateDrunkAdvice = new MateDrunkAdvice(receptor, drinker);
		this.clientApi.sendAdvice(mateDrunkAdvice);
	}

	/**
	 * @param clientApi The client API instance to set.
	 */
	public void setClientApi(ClientApi clientApi) {
		this.clientApi = clientApi;
	}

	/**
	 * @return The client API instance used.
	 */
	public ClientApi getClientApi() {
		return clientApi;
	}
}